package com.allways.dubbo.mdm.service.dataChangeLog.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.allways.dubbo.mdm.exception.MdmException;
import com.allways.dubbo.mdm.exception.MdmExceptionCode;
import com.allways.dubbo.mdm.service.BaseDataUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import com.allways.dubbo.base.dao.MdmBaseDaoImpl;
import com.allways.dubbo.mdm.beans.dataChangeLog.DataChangeLog;
import com.allways.dubbo.mdm.service.dataChangeLog.IDataChangeLogService;

/**
 * <p>ClassName: IDataChangeLogService</p>
 * <p>Description: 数据变更日志Service接口</p>
 * <p>Author: weih</p>
 * <p>Date: 2016-09-29</p>
 */
@Service("mdmDataChangeLogService")
public class DataChangeLogServiceImpl extends MdmBaseDaoImpl implements IDataChangeLogService {
	Log log = LogFactory.getLog(DataChangeLogServiceImpl.class);

    @Autowired
    BaseDataUtils baseDataUtils;
    
    @Override
    public int addDataChangeLog(DataChangeLog dataChangeLog) {
   		log.debug("For the new dataChangeLog parameters:"+ dataChangeLog.toJson());
   		validColumnLen(dataChangeLog);
    	
        //无code的不能插入
        if(StringUtils.isEmpty(dataChangeLog.getDataChangeLogCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        //code重复的不允许插入
        if(null != getDataChangeLogByCode(dataChangeLog.getDataChangeLogCode())){
            throw new MdmException(MdmExceptionCode.CODE_REPEATED,null);
        }
        
        return insert("dataChangeLog.insertDataChangeLog",dataChangeLog);
    }

    @Override
    public int updateDataChangeLog(DataChangeLog dataChangeLog) {
    	log.debug("Update for the dataChangeLog parameters:"+ dataChangeLog.toJson());
    	validColumnLen(dataChangeLog);
     	//无code的不能更新
        if(StringUtils.isEmpty(dataChangeLog.getDataChangeLogCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        //code记录不存在
        DataChangeLog oldDataChangeLog = getDataChangeLogByCode(dataChangeLog.getDataChangeLogCode());
        if(null == oldDataChangeLog){
            throw new MdmException(MdmExceptionCode.UNKNOWNED_RESULT,null);
        }
		oldDataChangeLog.setLogId(dataChangeLog.getLogId());
		oldDataChangeLog.setDataType(dataChangeLog.getDataType());
		oldDataChangeLog.setDataPk(dataChangeLog.getDataPk());
		oldDataChangeLog.setModifyTime(dataChangeLog.getModifyTime());
		oldDataChangeLog.setModifyType(dataChangeLog.getModifyType());
        
        return update("dataChangeLog.updateDataChangeLog",oldDataChangeLog);
    }

    @Override
    public int deleteDataChangeLog(DataChangeLog dataChangeLog) {
    	log.debug("Delete for the dataChangeLog parameters:"+ dataChangeLog.toJson());
     	//无code的不能删除
        if(StringUtils.isEmpty(dataChangeLog.getDataChangeLogCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        
        return delete("dataChangeLog.deleteDataChangeLog",dataChangeLog);
    }

    @Override
    public DataChangeLog getDataChangeLogByCode(String code) {
    	log.debug("Query for dataChangeLog code:" + code);
        return queryForObject("dataChangeLog.getDataChangeLogByCode",code);
    }
    @Override
    public long getDataChangeLogCount(DataChangeLog dataChangeLog) {
    	log.debug("Query for dataChangeLog count parameters:" + dataChangeLog.toJson());
        return queryForObject("dataChangeLog.getDataChangeLogCount",dataChangeLog);
    }
   @Override
    public List<DataChangeLog> queryDataChangeLogs(DataChangeLog dataChangeLog, int firstRow, int lastRow) {
      	log.debug("Query for dataChangeLog parameters:" + dataChangeLog.toJson()+"\r\n,Page parameters:["+firstRow+","+lastRow+"]");
        if(firstRow<0 || firstRow>lastRow || lastRow<0){
            throw new RuntimeException("Digital is not logical");
        }
        if(lastRow-firstRow > MAX_RETURN_NUM){
            lastRow = firstRow + MAX_RETURN_NUM;
        }
        Map<String ,Object> param = new HashMap<String,Object>();
        param.put("dataChangeLog",dataChangeLog);
        param.put("firstRow",firstRow);
        param.put("rowSize",lastRow-firstRow);
        return queryList("dataChangeLog.queryDataChangeLogs", param);
    }
    
    /**
     * 验证字段长度
     * @param dataChangeLog 需要验证的对象
     */
    private void validColumnLen(DataChangeLog dataChangeLog){
		if(!StringUtils.isEmpty(dataChangeLog.getLogId()) && dataChangeLog.getLogId().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(dataChangeLog.getDataType()) && dataChangeLog.getDataType().length()>50){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{50},null);
        }
		if(!StringUtils.isEmpty(dataChangeLog.getDataPk()) && dataChangeLog.getDataPk().length()>50){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{50},null);
        }
		if(!StringUtils.isEmpty(dataChangeLog.getModifyTime()) && dataChangeLog.getModifyTime().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(dataChangeLog.getModifyType()) && dataChangeLog.getModifyType().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
        
        //数据来源不明的不能操作
        if(!baseDataUtils.isDataSourceAvailable(dataChangeLog.getDataSource())){
            throw new MdmException(MdmExceptionCode.UNKNOWNED_DATASOURCE,null);
        }
    }
    
}
